import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86183
 * Date: 2024-02-12
 * Time: 8:50
 */
public class Sort {
    // 插入排序
    public static void insertSort(int[] array) {
        for (int i = 1; i < array.length; i++) {
            int tmp = array[i];
            int j = i - 1;
            for (; j >= 0; j--) {
                if (array[j] > tmp) {
                    array[j + 1] = array[j];
                } else {
                    array[j + 1] = tmp;
                    break;
                }
            }
            array[j + 1] = tmp;
        }
    }


    // 希尔排序
    public static void shellSort(int[] array) {
        int gap = array.length;
        while (gap != 1) {
            gap = gap / 2;

            shell(array, gap);
        }
        // write code  here
    }

    private static void shell(int[] array, int gap) {
        for (int i = gap; i < array.length; i++) {
            int tmp = array[i];
            int j = i - gap;
            for (; j >= 0; j -= gap) {
                if (array[j] > tmp) {
                    array[j + gap] = array[j];
                } else {
                    array[j + gap] = tmp;
                    break;
                }
            }
            array[j + gap] = tmp;
        }
    }

    // 选择排序
    public static void selectSort(int[] array) {
        for (int i = 0; i < array.length - 1; i++) {
            int tmp = i;
            for (int j = i + 1; j < array.length; j++) {
                if (array[j] < array[tmp]) {
                    tmp = j;
                }

            }
            swap(array, i, tmp);

        }
        // write code  here
    }

    private static void swap(int[] array, int i, int j) {
        int t = array[i];
        array[i] = array[j];
        array[j] = t;
    }

    public static void selectSort2(int[] array) {
        int left = 0;
        int right = array.length - 1;
        while (left < right) {
            int min = left;
            int max = left;
            for (int i = left + 1; i <= right; i++) {
                if (array[i] < array[min]) {
                    min = i;
                }
                if (array[i] > array[max]) {
                    max = i;
                }
            }
            swap(array, left, min);
            if (left == max) {
                max = min;
            }
            swap(array, right, max);
            left++;
            right--;
        }
    }


    // 堆排序
    public static void heapSort(int[] array) {
        //创建大根堆
        for (int parent = (array.length - 1 - 1) / 2; parent >= 0; parent--) {
            createHeap(array, parent);
        }
        int end = array.length - 1;
        while (end > 0) {
            swap(array, 0, end);
            shiftDown(array, 0, end);
            end--;
        }


    }

    private static void createHeap(int[] array, int parent) {
        int child = 2 * parent + 1;
        while (child < array.length) {
            if (child + 1 < array.length - 1 && array[child] < array[child + 1]) {
                child++;
            }
            if (array[parent] < array[child]) {
                swap(array, parent, child);
                parent = child;
                child = 2 * parent + 1;
            } else {
                break;
            }

        }
    }

    private static void shiftDown(int[] array, int i, int j) {
        int parent = i;
        int child = 2 * parent + 1;
        while (child < j) {
            if (child + 1 < j && array[child] < array[child + 1]) {
                child++;
            }
            if (array[parent] < array[child]) {
                swap(array, parent, child);
                parent = child;
                child = 2 * parent + 1;

            } else {
                break;
            }
        }


    }
    // 冒泡排序

    public static void bubbleSort(int[] array) {
        for (int i = 0; i < array.length - 1; i++) {
            boolean flg = true;
            for (int j = 0; j < array.length - 1 - i; j++) {
                if (array[j] > array[j + 1]) {
                    swap(array, j, j + 1);
                    flg = false;
                }
            }
            if (flg == true) {
                break;
            }

        }

    }

    public static void quickSort(int[] array){
        // write code  here
        quick(array,0,array.length-1);


    }
    private static void quick(int[] array,int left,int right){
        if(left>=right){
            return;
        }
        //三数取中法

        int mid=left+(right-left)/2;
        mid = finIndex(array,left,right,mid);

        if(right-left+1<10){
            insertSort2(array,left,right);
        }
        int pivot=partition(array,left,right);
        quick(array,left,pivot-1);
        quick(array,pivot+1,right);
    }
    public static void insertSort2(int[] array,int left,int right) {
        for (int i = left+1; i <right; i++) {
            int tmp = array[i];
            int j = i - 1;
            for (; j >= left; j--) {
                if (array[j] > tmp) {
                    array[j + 1] = array[j];
                } else {
                    array[j + 1] = tmp;
                    break;
                }
            }
            array[j + 1] = tmp;
        }
    }

    private static int finIndex(int[] array,int left,int right,int mid){
        if(array[left]<array[right]){
            if(array[mid]>array[right]){
                return right;
            }else if(array[left]>array[mid]){
                return left;
            }else{
                return mid;
            }
        }else{
            if(array[right]>array[mid]){
                return right;
            }else if(array[left]<array[mid]){
                return left;
            }else {
                return mid;
            }
        }
    }
    private static int partitionHoare(int[] array,int left,int right){
        int tmp=array[left];
        int i=left;
        while(left<right){
            while(left<right&&array[right]>=tmp){
                right--;
            }
            while(left<right&&array[left]<=tmp){
                left++;
            }
            if(left<right){
                swap(array,left,right);
            }

        }
        swap(array,i,left);
        return left;

    }
    private static int partition(int[] array,int left,int right){
        int tmp=array[left];
        while(left<right){
            if (array[right] >=tmp) {
                right--;
            }
            array[left]=array[right];
            if(array[left]<+tmp){
                left++;
            }
            array[right]=array[left];
        }
        array[left]=tmp;
        return left;
    }

    /**
     * 快速排序非递归
     * @param array
     */
    public static void quickSort2(int[] array){
        quick2(array,0,array.length-1);

    }
    private static void quick2(int[] array,int left,int right){
        int pivot=partition(array,left,right);
        Stack<Integer> stack=new Stack<>();
        if(pivot-1>left){
            stack.push(left);
            stack.push(pivot-1);
        }
        if(pivot+1<right){
            stack.push(pivot+1);
            stack.push(right);
            }
        while(!stack.isEmpty()){
            right=stack.pop();
            left=stack.pop();
            pivot=partition(array, left, right);
            if(pivot-1>left){
                stack.push(left);
                stack.push(pivot-1);
            }
            if(pivot+1<right){
                stack.push(pivot+1);
                stack.push(right);
            }
        }
    }
    // 归并排序---递归
      public static void mergeSort(int[] array){
        merge(array,0,array.length-1);
    }
    private static void merge(int[] array,int left,int right){
        if(left>=right){
            return;
        }
        int mid=left+(right-left)/2;
        merge(array,left,mid);
        merge(array,mid+1,right);
        fun(array,left,mid,mid+1,right);
    }

    private static void fun(int[] array,int s1,int e1,int s2,int e2) {
        int[] tmp = new int[e2 - s1 + 1];
        int index = s1;
        int k = 0;
        while (s1 <= e1 && s2 <= e2) {
            if (array[s1] <= array[s2]) {
                tmp[k++] = array[s1];
                s1++;
            } else {
                tmp[k++] = array[s2];
                s2++;
            }
        }
        while (s1 <= e1) {
            tmp[k++] = array[s1];
            s1++;
        }
        while (s2 <= e2) {
            tmp[k++] = array[s2];
            s2++;
        }
        for (int i = 0; i < k; i++) {
            array[i + index] = tmp[i];
        }
    }
}

